/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.opml.core.tests;

import java.io.InputStream;

import junit.framework.TestCase;
import net.jmesnil.opml.core.IBody;
import net.jmesnil.opml.core.IHead;
import net.jmesnil.opml.core.IOPML;
import net.jmesnil.opml.core.IOutline;
import net.jmesnil.opml.core.IOutlineNode;
import net.jmesnil.opml.core.IOwner;
import net.jmesnil.opml.core.OPMLException;
import net.jmesnil.opml.core.OPMLPlugin;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;

public class OPMLTestCase extends TestCase {
    public void testFromNullOPMLString() throws Exception {
        try {
            OPMLPlugin.getDefault().fromXml(null);
            fail("should throw an OPMLException if the String is null");
        } catch (OPMLException e) {
        }
    }

    public void testFromEmptyOPMLString() throws Exception {
        try {
            OPMLPlugin.getDefault().fromXml("");
            fail("should throw an OPMLException if the String is empty");
        } catch (OPMLException e) {
        }
    }

    public void testFromNotXMLString() throws Exception {
        try {
            OPMLPlugin.getDefault().fromXml("this is not a xml content");
            fail("should throw an OPMLException if the String does not contain XML");
        } catch (OPMLException e) {
        }
    }

    public void testFromValidXMLString() throws Exception {
        InputStream stream = FileLocator.openStream(OPMLPlugin.getDefault()
                .getBundle(), new Path("files/states.opml"), false);
        assertNotNull(stream);
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = stream.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        IOPML opml = OPMLPlugin.getDefault().fromXml(out.toString());
        assertNotNull(opml);
        IHead head = opml.getHead();
        assertNotNull(head);
        assertEquals("states.opml", head.getTitle());
        IBody body = opml.getBody();
        assertNotNull(body);
        IOutline[] outlines = body.getChildren();
        assertNotNull(outlines);
        assertTrue(outlines.length == 1);
        IOutline outline = (IOutline) outlines[0];
        IOutlineNode parent = outline.getParent();
        assertNotNull(parent);
        assertEquals(body, parent);
        assertEquals("United States", outline.getText());
        assertTrue(outline.getChildren().length > 0);
        IOutline nestedOutline = outline.getChildren()[0];
        assertNotNull(nestedOutline);
        assertEquals(outline, nestedOutline.getParent());
    }

    public void testAppendOutlineToBody() throws Exception {
        InputStream stream = FileLocator.openStream(OPMLPlugin.getDefault()
                .getBundle(), new Path("files/states.opml"), false);
        assertNotNull(stream);
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = stream.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        IOPML opml = OPMLPlugin.getDefault().fromXml(out.toString());
        assertNotNull(opml);
        IBody body = opml.getBody();
        assertNotNull(body);
        IOutline[] outlines = body.getChildren();
        assertNotNull(outlines);
        int initialSize = outlines.length;
        body.appendNewOutline("test");
        outlines = body.getChildren();
        assertEquals(initialSize + 1, outlines.length);
        IOutline lastOutline = outlines[outlines.length - 1];
        assertEquals("test", lastOutline.getText());
    }

    public void testCreateEmptyOPML() throws Exception {
        IOPML opml = OPMLPlugin.getDefault().createOPML();
        assertNotNull(opml);
        IHead head = opml.getHead();
        assertNotNull(head);
        IOwner owner = head.getOwner();
        assertNull(owner);
        IBody body = opml.getBody();
        assertNotNull(body);
        assertEquals(0, body.getChildren().length);
        String xml = opml.toXML();
        assertNotNull(xml);
    }

    public void testAddOutlineToEmptyOPML() throws Exception {
        IOPML opml = OPMLPlugin.getDefault().createOPML();
        assertNotNull(opml);
        IBody body = opml.getBody();
        assertNotNull(body);
        assertEquals(0, body.getChildren().length);
        body.appendNewOutline("test");
        assertEquals(1, body.getChildren().length);
        IOutline firstOutline = body.getChildren()[0];
        assertEquals("test", firstOutline.getText());
    }
}
